//
//  FileStorage.swift
//  PandaSwift
//
//  Created by 余意 on 2017/2/22.
//  Copyright © 2017年 余意. All rights reserved.
//

import Foundation

//MARK:- 获取plist文件内容
func readDictionaryPlistForKey(searchPathDirectory:FileManager.SearchPathDirectory,fileName:String) -> NSMutableDictionary
{
    let searchPath = NSSearchPathForDirectoriesInDomains(searchPathDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
    let plistPath = searchPath[0] + "/" + fileName + ".plist"
    let dic = NSMutableDictionary(contentsOfFile:plistPath)
    
    if dic == nil
    {
        return NSMutableDictionary()
    }
    return dic!
}

//MARK:- 获取Documents目录下文件路径
func documentFilePath(fileName:String) -> String
{
    // /Library/
    let path = NSHomeDirectory() + "/Documents/" + fileName
    return path
}

//MARK:- 判断文件夹是否存在
func fileExistAtPath(filePath:String) -> Bool
{
    if (FileManager.default.fileExists(atPath: filePath))
    {
        return true
    }
    return false
}

//MARK:- 删除文件
func deleteFile(filePath:String)
{
    if (fileExistAtPath(filePath: filePath))
    {
        let fileUrl = URL(fileURLWithPath:filePath)
        try! FileManager.default.removeItem(at: fileUrl)
    }
}

//MARK:- 创建文件夹
func creatFile(filePath:String)
{
    if !FileManager.default.fileExists(atPath: filePath)
    {
        let fileUrl = URL(fileURLWithPath:filePath)
        // TODO withIntermediateDirectories 参数为true 表示文件路径不存在 也会创建
        try! FileManager.default.createDirectory(at: fileUrl, withIntermediateDirectories: true, attributes: nil)
    }
}

//MARK:- 二进制写入文件
func writeToFile(data:Data,fileName:String)
{
    let filePath = documentFilePath(fileName: fileName)
    creatFile(filePath: filePath)
    let fileUrl = URL(fileURLWithPath:filePath)
    try! data.write(to: fileUrl)
}

//MARK:- 将资源文件的写入文件
func writeDataToFile(fileName:String,fileType:String,searchPathDirectory:FileManager.SearchPathDirectory)
{
    let searchPath = NSSearchPathForDirectoriesInDomains(searchPathDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
    let filePath = searchPath[0] + "/" + fileName + "." + fileType
    
    if  !FileManager.default.fileExists(atPath: filePath)
    {
        let defaultFilePath = Bundle.main.path(forResource: fileName, ofType: fileType)
        try! FileManager.default.copyItem(atPath: defaultFilePath!, toPath: filePath)
    }
}

//MARK:- 创建或向已存在的plist文件写数据
func updatePlist(searchPathDirectory:FileManager.SearchPathDirectory,fileName:String,infoDic:NSDictionary)
{
    let searchPath = NSSearchPathForDirectoriesInDomains(searchPathDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
    let plistPath = searchPath[0] + "/" + fileName + ".plist"
    if !FileManager.default.fileExists(atPath: plistPath)
    {
        FileManager.default.createFile(atPath: plistPath, contents: nil, attributes: nil)
    }
    infoDic.write(toFile: plistPath, atomically: true)
}

//MARK:- 获取plist文件根目录某个字段
func readPlistForKey(searchPathDirectory:FileManager.SearchPathDirectory,fileName:String,key:String) -> String
{
    let dic : NSMutableDictionary = readDictionaryPlistForKey(searchPathDirectory: searchPathDirectory, fileName: fileName)
    let keyString = dic.object(forKey: key) as! String
    return keyString
}

//MARK:- 更新plist文件根目录某个字段对应的数据
func updatePlist(searchPathDirectory:FileManager.SearchPathDirectory,fileName:String,key:String,object:Any)
{
    let dic : NSMutableDictionary = readDictionaryPlistForKey(searchPathDirectory: searchPathDirectory, fileName: fileName)
    dic.setValue(object, forKey: key)
    updatePlist(searchPathDirectory: searchPathDirectory, fileName: fileName, infoDic: dic)
}



let USER_INFO          = "userInfo"
let USER_SETTING_INFO  = "userSettingInfo"

// localUserInfo.plist
let USER_INFO_LOCAL    = "localUserInfo"

// MARK: - 保存登录信息，实际上是存在LocalUserInfo.plist文件里面，infoKey是对应Key
func saveInfoToLocal(info:Any , infoKey:String)
{
    var userInfoDic = NSMutableDictionary()
    userInfoDic = readDictionaryPlistForKey(searchPathDirectory: .documentDirectory, fileName: USER_INFO_LOCAL)
    userInfoDic.setValue(info, forKey: infoKey)
    updatePlist(searchPathDirectory: .documentDirectory, fileName: USER_INFO_LOCAL, infoDic: userInfoDic)
}

//MARK: - 根据key来查找存放在LocalUserInfo.plist文件对应的数据
func infoFromLocal(infoKey:String) -> Any
{
    let userInfoDic : NSMutableDictionary = readDictionaryPlistForKey(searchPathDirectory: .documentDirectory, fileName: USER_INFO_LOCAL)
    let object = userInfoDic.object(forKey: infoKey) as Any
    return object
}

//MARK: - 保存登录时的用户信息
func saveUserInfoToLocal(_ userInfoDic : NSDictionary)
{
    saveInfoToLocal(info: userInfoDic, infoKey: USER_INFO)
}

//MARK: - 返回保留的登录信息
func userInfoFromLocal() -> NSDictionary
{
    let userInfoDic : NSDictionary = infoFromLocal(infoKey: USER_INFO) as! NSDictionary
    return userInfoDic
}

//MARK: - 保存本地设置
func saveSettingInfoToLocal(_ userInfoDic:NSDictionary)
{
    saveInfoToLocal(info: userInfoDic, infoKey: USER_SETTING_INFO)
}

//MARK: - 返回本地设置
func userSettingInfoFromLocal() -> NSDictionary
{
    let userInfoDic : NSDictionary = infoFromLocal(infoKey: USER_SETTING_INFO) as! NSDictionary
    return userInfoDic
}






















